﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class ItemNo : BasePage
    {
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected HtmlForm F2671;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnItemNoID;
        protected HtmlHead Head1;
        protected Label lblDescr;
        protected Label lblItemNo;
        protected Label lblItemNoID;
        protected Label lblItemNoUom;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected TextBox txtDescr;
        protected TextBox txtItemNoID;
        protected TextBox txtItemNoUom;

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.hdnFlag.Value != "1")
            {
                this.lblMsg.Text = "没有保存不能删除！";
            }
            else
            {
                try
                {
                    List<string> ItemNoID = new List<string> {
                        this.txtItemNoID.Text.Trim()
                    };
                    this.lblMsg.Text = "删除项号 " + BaseInfoServiceClientProxy.GetInstance().Delete(ItemNoID, "ITEM_NO");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    base.DataEntityViewState = null;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                }
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            base.Response.Redirect("ItemNoList.aspx?from=edit", false);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Exception ex;
            try
            {
                if (string.IsNullOrEmpty(this.txtDescr.Text))
                {
                    this.lblMsg.Text = "项号描述不能为空!";
                }
                else
                {
                    ItemNoInfo itemNoInfo = null;
                    if (base.DataEntityViewState != null)
                    {
                        itemNoInfo = base.DataEntityViewState as ItemNoInfo;
                    }
                    else
                    {
                        itemNoInfo = new ItemNoInfo();
                    }
                    itemNoInfo.ItemNoUom = this.txtItemNoUom.Text.Trim();
                    itemNoInfo.Descr = this.txtDescr.Text.Trim();
                    itemNoInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                    if (this.hdnFlag.Value == "1")
                    {
                        itemNoInfo.ItemNoID = this.txtItemNoID.Text.Trim();
                        itemNoInfo.UpdatedBy = base.UserInfo.UserID;
                        this.lblMsg.Text = "更新" + BaseInfoServiceClientProxy.GetInstance().Update(itemNoInfo, "ITEM_NO") + "成功";
                    }
                    else
                    {
                        try
                        {
                            itemNoInfo.ItemNoID = this.txtItemNoID.Text.Trim();
                            itemNoInfo.CreatedBy = base.UserInfo.UserID;
                            this.txtItemNoID.Text = BaseInfoServiceClientProxy.GetInstance().Insert(itemNoInfo, "ITEM_NO");
                        }
                        catch (Exception exception1)
                        {
                            ex = exception1;
                            this.lblMsg.Text = "新增失败，项号：" + this.txtItemNoID.Text.Trim() + "已存在";
                            return;
                        }
                        this.lblMsg.Text = "新增" + this.txtItemNoID.Text + "成功";
                        base.DataEntityViewState = itemNoInfo;
                        this.hdnFlag.Value = "1";
                    }
                    this.txtItemNoID.Enabled = false;
                }
            }
            catch (Exception exception2)
            {
                ex = exception2;
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnItemNoID.Value = base.Request.QueryString["ID"];
                this.hdnFlag.Value = "1";
                this.SetItemNoInfo2Form();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetItemNoInfo2Form()
        {
            string ItemNoID = this.hdnItemNoID.Value;
            ItemNoInfo itemNoInfo = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(ItemNoID, "ITEM_NO") as ItemNoInfo;
            base.DataEntityViewState = itemNoInfo;
            if (itemNoInfo != null)
            {
                this.txtItemNoID.Text = itemNoInfo.ItemNoID;
                this.txtItemNoUom.Text = itemNoInfo.ItemNoUom;
                this.txtDescr.Text = itemNoInfo.Descr;
                this.chkIsActive.Checked = itemNoInfo.IsActive == "Y";
            }
            this.txtItemNoID.Enabled = false;
        }
    }
}

